#include "widget.h"
#include <QScrollArea>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowTitle("⾃定义控件-仪表盘");
    this->setMinimumWidth(800);
    this->setMinimumHeight(600);

    // 整体采用垂直布局
    QVBoxLayout* vLayout = new QVBoxLayout(this);
    vLayout->setContentsMargins(0, 0, 0, 0);

    // 使用QScrollArea可以滚动显示
    QScrollArea* scrollArea = new QScrollArea(this);
    scrollArea->setWidgetResizable(true);

    QWidget* scrollAreaContents = new QWidget(this);

    _gridLayout = new QGridLayout(scrollAreaContents);
    _gridLayout->setSpacing(0);
    _gridLayout->setContentsMargins(0, 0, 0, 0);

    scrollArea->setWidget(scrollAreaContents);
    vLayout->addWidget(scrollArea);

    addGauge00();
    addGauge01();
    addGauge02();
    addGauge10();
    addGauge11();
    addGauge12();

    _timer = new QTimer(this);
    connect(_timer, &QTimer::timeout, this, &Widget::timeout);
    _timer->start(50);
}

Widget::~Widget() {}

void Widget::addGauge00()
{
    _gauge = new Gauge(this);
    _gauge->setMinimumWidth(400);
    _gauge->setMinimumHeight(300);
    _gauge->setStyleSheet("background-color: rgb(240, 240, 240)");
    _gridLayout->addWidget(_gauge, 0, 0);

    _gauge->setStartAngle(315);
    _gauge->setPenCapStyle(Qt::FlatCap);
    _gauge->setScalePosition(Gauge::ON);
    _gauge->setMinValue(-50);
    _gauge->setCurrentRing(true);
    _gauge->setCurrentRingColor(QColor(89, 113, 201));

    _gauge->setTitle("温度");
    _gauge->setUnit("°C");
}

void Widget::addGauge01()
{
    Gauge* gauge = new Gauge(this);
    gauge->setMinimumWidth(400);
    gauge->setMinimumHeight(300);
    gauge->setStyleSheet("background-color: rgb(200, 200, 200)");
    _gridLayout->addWidget(gauge, 0, 1);

    gauge->setStartAngle(135);
    gauge->setRingWidth(5);
    gauge->setPenCapStyle(Qt::FlatCap);
    gauge->setScalePosition(Gauge::OUTSIDE);
    gauge->setMinValue(0);
    gauge->setMaxValue(100);
    gauge->setCurrentRing(true);
    gauge->setCurrentRingColor(QColor(89, 113, 201));
    gauge->setPointerColor(QColor(245, 199, 78));

    gauge->setTitle("湿度");
    gauge->setUnit("%");

}

void Widget::addGauge02()
{
    Gauge* gauge = new Gauge(this);
    gauge->setMinimumWidth(400);
    gauge->setMinimumHeight(300);
    gauge->setStyleSheet("background-color: rgb(150, 150, 150)");
    _gridLayout->addWidget(gauge, 0, 2);

    gauge->setStartAngle(45);
    gauge->setRingWidth(15);
    //gauge->setRingColor(QColor(205, 190, 190));
    gauge->setPenCapStyle(Qt::RoundCap);
    gauge->setScalePosition(Gauge::OUTSIDE_GAP);
    gauge->setMinValue(0.0f);
    gauge->setMaxValue(1.0f);
    gauge->setTargetValue(0.5f);
    gauge->setPrecision(1);
    gauge->setPointerColor(QColor(89, 113, 208));
    gauge->setCurrentRing(true);
    gauge->setCurrentRingColor(QColor(89, 113, 201));

    gauge->setTitle("电压");
    gauge->setUnit("V");
}

void Widget::addGauge10()
{
    Gauge* gauge = new Gauge(this);
    gauge->setMinimumWidth(400);
    gauge->setMinimumHeight(300);
    gauge->setStyleSheet("background-color: rgb(100, 100, 100)");
    _gridLayout->addWidget(gauge, 1, 0);

    gauge->setStartAngle(315);
    gauge->setPenCapStyle(Qt::FlatCap);

    gauge->setTitle("H2浓度");
    gauge->setUnit("");
}

void Widget::addGauge11()
{
    Gauge* gauge = new Gauge(this);
    gauge->setMinimumWidth(400);
    gauge->setMinimumHeight(300);
    gauge->setStyleSheet("background-color: rgb(80, 80, 80)");
    _gridLayout->addWidget(gauge, 1, 1);

    gauge->setStartAngle(225);
    gauge->setRingWidth(5);
    gauge->setPenCapStyle(Qt::RoundCap);

    gauge->setTitle("温度");
    gauge->setUnit("°C");
}

void Widget::addGauge12()
{
    Gauge* gauge = new Gauge(this);
    gauge->setMinimumWidth(400);
    gauge->setMinimumHeight(300);
    gauge->setStyleSheet("background-color: rgb(50, 50, 50)");
    _gridLayout->addWidget(gauge, 1, 2);

    gauge->setStartAngle(45);
    gauge->setRingWidth(15);
    gauge->setPenCapStyle(Qt::MPenCapStyle);
    gauge->setPointerStyle(Gauge::CIRCLE);

    gauge->setTitle("电流");
    gauge->setUnit("A");
}

void Widget::timeout()
{
    int min = _gauge->getMinValue();
    int max = _gauge->getMaxValue();
    int cur = _gauge->getTargetValue();

    if (_inc) {
        if (cur < max) {
            _gauge->setTargetValue(cur + 1);
        } else {
            _inc = false;
        }
    } else {
        if (cur > min) {
            _gauge->setTargetValue(cur - 1);
        } else {
            _inc = true;
        }
    }
}
